﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;
using System.Drawing;

namespace WXYW
{
    public partial class RWGLDetailReadOnly : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TaskService service = new TaskService();
                Task task = service.GetTaskByTaskID(Convert.ToInt32(Request.QueryString["TID"]));
                if (!task.User1Reference.IsLoaded)
                {
                    task.User1Reference.Load();
                }
                if (!task.UserReference.IsLoaded)
                {
                    task.UserReference.Load();
                }
                subjectLabel.Text = task.Subject;
                createrLabel.Text = task.User1.ContactPerson;
                createdateLabel.Text = task.CreateDate.Value.ToString("yyyy-MM-dd");
                shouldLabel.Text = task.ShouldCompleteDate.Value.ToString("yyyy-MM-dd");
                taskContentTxtBox.Text = task.Content;
                assigneeLabel.Text = task.User.ContactPerson;
                completeDateLbl.Text = task.CompleteDate.HasValue ? task.CompleteDate.Value.ToString("yyyy-MM-dd") : "";
                taskTypeLabel.Text = task.TaskType;
                resultTextBox.Text = task.Result;
                resultTypeLbl.Text = task.ResultType;                
                hfTextBox.Text = task.Feedback;
                questionTypeLbl.Text = task.ReasonType;
                statusLbl.Text = task.Status;
                if (task.FeedbackDate.HasValue)
                {
                    feedbackTimeLbl.Text = task.FeedbackDate.Value.ToString("yyyy-MM-dd");
                }
                //feedbackDateTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
                if (!task.TaskFile.IsLoaded)
                {
                    task.TaskFile.Load();
                }
                if (task.TaskFile.Count() > 0)
                {
                    taskFileLnkBtn.ForeColor = Color.Gray;
                }
            }
        }

        protected void backLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/RWGLSearch.aspx");
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<TaskFile> dds = new List<TaskFile>();
            dds.Add(new TaskFile());
            return dds;
        }

        protected void TaskFileGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                ((HyperLink)e.Row.FindControl("ViewFileHyperLink")).NavigateUrl = string.Format("~/TaskFile/{0}_{1}", ((TaskFile)e.Row.DataItem).ID, ((TaskFile)e.Row.DataItem).FileName);
            }
        }

        protected void ModalPopupExtender1_Load(object sender, EventArgs e)
        {
            TaskService taskService = new TaskService();
            TaskFileGridView.DataSource = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
            TaskFileGridView.DataBind();
        }

        protected void uploadTaskFileBtn_Click(object sender, EventArgs e)
        {
            if (TaskFileFileUpload.HasFile)
            {
                TaskService taskService = new TaskService();
                long fileID = taskService.AddTaskFile(TaskFileFileUpload.FileName, Convert.ToInt32(Request.QueryString["TID"]));
                TaskFileFileUpload.SaveAs(Server.MapPath(string.Format("TaskFile/{0}_{1}", fileID, TaskFileFileUpload.FileName)));

                IQueryable<TaskFile> files = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
                TaskFileGridView.DataSource = files;
                TaskFileGridView.DataBind();

                TaskFileModalPopupExtender.Show();
            }
        }

        protected void taskFileLnkBtn_Click(object sender, EventArgs e)
        {
            TaskService taskService = new TaskService();
            TaskFileGridView.DataSource = taskService.GetTaskFile(Convert.ToInt32(Request.QueryString["TID"]));
            TaskFileGridView.DataBind();

            TaskFileModalPopupExtender.Show();
        }
    }
}
